package com.android.widget.gesture;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;

import com.android.widget.demo.R;

public class GestureTest extends Activity {
	final int RIGHT = 0;
	final int LEFT = 1;

	private GestureDetector mGestureDetector;

	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_gesture);

		mGestureDetector = new GestureDetector(this, onGestureListener);
	}

	private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			float x = e2.getX() - e1.getX();
			float y = e2.getY() - e1.getY();

			if (x > 0) {
				doResult(RIGHT);
			} else if (x < 0) {
				doResult(LEFT);
			}
			return true;
		}
	};

	public boolean onTouchEvent(MotionEvent event) {
		return mGestureDetector.onTouchEvent(event);
	}

	public void doResult(int action) {

		switch (action) {
		case RIGHT:
			Toast.makeText(this, "right!", Toast.LENGTH_LONG).show();
			Log.d("linc", "right!!");
			break;

		case LEFT:
			Toast.makeText(this, "left!", Toast.LENGTH_LONG).show();
			Log.d("linc", "left!!");
			break;

		}
	}

}
